<template>
    <div>
        <div class="container">
            <!-- 头部 -->
            <div class="nav">
                <div class="nav_wrap">
                    <div class="quit" @click="$router.back()"></div>
                    <span>书库</span>
                    <div class="nav_icon">
                        <router-link tag="a" to="/shelf"></router-link>
                        <i v-show="!isTop" @click="topShow"></i>
                        <i class="topnav" v-show="isTop" @click="topShow"></i>
                    </div>
                </div>
            </div>
            <div class="wrap" v-show="isTop" @click.self="topShow">
                <div class="subnav">
                    <ul>
                        <router-link tag="li" to="/">
                            <img src="../assets/images/tp_1.png" alt="">
                            <span>首页</span>
                        </router-link>
                        <router-link tag="li" to="/library">
                            <img src="../assets/images/tp_02.png" alt="">
                            <span>书库</span>
                        </router-link>
                        <router-link tag="li" to="/shelf">
                            <img src="../assets/images/tp_03.png" alt="">
                            <span>书架</span>
                        </router-link>
                        <router-link tag="li" to="/user">
                            <img src="../assets/images/tp_04.png" alt="">
                            <span>账户</span>
                        </router-link>
                        <router-link tag="li" to="/pay">
                            <img src="../assets/images/tp_05.png" alt="">
                            <span>充值</span>
                        </router-link>
                        <router-link tag="li" to="/search">
                            <img src="../assets/images/tp_06.png" alt="">
                            <span>搜索</span>
                        </router-link>
                    </ul>
                </div>
            
            </div>
            <!-- 按类型选书 -->
            <div class="bookType bor_bot">
                <ul>
                    <li>
                        <span class="tit">类型：</span>
                        <ul>
                            <li :class="[bookSex == 0?'con':'']" @click="bookSwitch(0,'sex')"><span>不限</span></li>
                            <li :class="[bookSex == 1?'con':'']" @click="bookSwitch(1,'sex')"><span>男频</span></li>
                            <li :class="[bookSex == 2?'con':'']" @click="bookSwitch(2,'sex')"><span>女频</span></li>
                            <li :class="[bookSex == 3?'con':'']" @click="bookSwitch(3,'sex')"><span>悬疑</span></li>
                        </ul>
                    </li>
                    <li>
                        <span class="tit">字数：</span>
                        <ul>
                            <li :class="[bookFont == 0?'con':'']" @click="bookSwitch(0,'font'),bookFontCup('all')"><span>不限</span></li>
                            <li :class="[bookFont == 1?'con':'']" @click="bookSwitch(1,'font'),bookFontCup(300)"><span>300万以下</span></li>
                            <li :class="[bookFont == 2?'con':'']" @click="bookSwitch(2,'font'),bookFontCup(500)"><span>300-500万</span></li>
                            <li :class="[bookFont == 3?'con':'']" @click="bookSwitch(3,'font'),bookFontCup(1000)"><span>500-1000万</span></li>
                            <li :class="[bookFont == 4?'con':'']" @click="bookSwitch(4,'font'),bookFontCup()"><span>1000万以上</span></li>
                        </ul>
                    </li>
                    <!-- <li>
                        <span class="tit">排序：</span>
                        <ul>
                            <li :class="[bookSort == 0?'con':'']" @click="bookSwitch(0,'sort')"><span>不限</span></li>
                            <li :class="[bookSort == 1?'con':'']" @click="bookSwitch(1,'sort')"><span>最新更新</span></li>
                            <li :class="[bookSort == 2?'con':'']" @click="bookSwitch(2,'sort')"><span>订阅排行</span></li>
                            <li :class="[bookSort == 3?'con':'']" @click="bookSwitch(3,'sort')"><span>点击排行</span></li>
                        </ul>
                    </li> -->
                    <!-- <li>
                        <span class="tit">更新：</span>
                        <ul>
                            <li :class="[bookUpdata == 0?'con':'']" @click="bookSwitch(0,'updata')"><span>不限</span></li>
                            <li :class="[bookUpdata == 1?'con':'']" @click="bookSwitch(1,'updata')"><span>三日内</span></li>
                            <li :class="[bookUpdata == 2?'con':'']" @click="bookSwitch(2,'updata')"><span>七日内</span></li>
                            <li :class="[bookUpdata == 3?'con':'']" @click="bookSwitch(3,'updata')"><span>半月内</span></li>
                            <li :class="[bookUpdata == 4?'con':'']" @click="bookSwitch(4,'updata')"><span>一月内</span></li>
                        </ul>
                    </li> -->
                    <li>
                        <span class="tit">收费：</span>
                        <ul>
                            <li :class="[bookCharge == 0?'con':'']" @click="bookSwitch(0,'charge'),bookPayMoney('all')"><span>不限</span></li>
                            <li :class="[bookCharge == 1?'con':'']" @click="bookSwitch(1,'charge'),bookPayMoney('free')"><span>免费</span></li>
                            <li :class="[bookCharge == 2?'con':'']" @click="bookSwitch(2,'charge'),bookPayMoney('pay')"><span>收费</span></li>
                        </ul>
                    </li>
                    <li>
                        <span class="tit">状态：</span>
                        <ul>
                            <li :class="[bookState == 0?'con':'']" @click="bookSwitch(0,'state'),bookPayType('all')"><span>不限</span></li>
                            <li :class="[bookState == 1?'con':'']" @click="bookSwitch(1,'state'),bookPayType('open')"><span>未完结</span></li>
                            <li :class="[bookState == 2?'con':'']" @click="bookSwitch(2, 'state'), bookPayType('end')"><span>已完结</span></li>
                        </ul>
                    </li>
                   
                </ul>
            </div>

            <!-- 书单 -->
            <div class="list_books bor_bot">  
                <div class="books_cont" v-if="booksData">
                    <ul>
                        <router-link tag="li" :to="{ path: '/about', query: { userId: item.id } }"
                            v-for="(item,index) in booksData" :key="index">
                            <div class="img">
                                <img :src="item.iconUrl" alt="">
                            </div>
                            <div class="intro">
                                <div class="in_tit">
                                    {{item.title}}
                                </div>
                                <p>
                                    {{ item.description }}
                                </p>
                                <div class="in_foot">
                                    <div class="state">
                                        <span>{{ item.bookStatus ? '完结' : '连载'}}</span>
                                    </div>
                                    <div class="book_type">
                                        <span class="book_num">{{ item.showTotalCount }}</span>
                                        <span>{{ item.category }}</span>
                                    </div>
                                </div>
                            </div>
                        </router-link>
                    </ul>
                </div>
            </div>
            <!-- 搜索框 -->
            <router-link tag="div" to="/search" class="search">
                <div class="search_wrap">
                    <input type="text">
                    <button></button>
                </div>
            </router-link>
            <!-- 页脚信息 -->
            <div class="footer">
                <div class="foot_tit">
                    <ul>
                        <li>
                            <a href="javascript:;">首页</a>
                        </li>
                        <li>
                            <a href="javascript:;">书库</a>
                        </li>
                        <li>
                            <a href="javascript:;">书架</a>
                        </li>
                        <li>
                            <a href="javascript:;">账户</a>
                        </li>
                        <li>
                            <a href="javascript:;">客户端</a>
                        </li>
                    </ul>
                </div>
                <div class="foot_cont">
                    <div class="foot_info">
                        <p>客服QQ群：326738643</p>
                        <p>客服电话：010-86462015-800</p>
                        <p>©2020北京爱书客科技有限公司</p>
                        <p>京ICP备13037522号</p>
                    </div>
                    <div class="info_img">
                        <img src="../assets/images/ledu.png" alt="">
                        <span>乐读书城</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { getJsonData, getSortData } from '../api/home';
    export default {
        data(){
            return{
                booksData:null,//书单信息
                booksCopy:null,//书单信息副本
                isTop: false,//判断子菜单的展示
                booksSort:null,//女频
                bookState:0,//状态栏切换
                bookCharge:0,//收费栏切换
                bookUpdata:0,//更新栏切换
                bookSort:0,//排序栏切换
                bookFont:0,//字数栏切换
                bookSex:0,//类型栏切换
                typeArr: {//存储按条件筛选的条件
                    font:[],
                    charge:[],
                    type:[],
                },
            }
        },
        created() {
            getJsonData().then(data => {
                this.booksData = data.data.books;
                this.booksCopy = data.data.books;
            })
            getSortData().then(data => {
                this.booksSort = data.data.books
            })
        },
        methods:{
            bookSwitch(ind,type){//按键底色切换
                if(type == 'state'){
                    this.bookState = ind;
                    
                } else if (type == 'charge'){
                    this.bookCharge = ind;

                } else if(type == 'updata'){
                   this.bookUpdata = ind;

                } else if(type == 'sort'){
                    this.bookSort = ind;

                } else if(type == 'font'){
                    this.bookFont = ind;

                } else if(type == 'sex'){
                    this.bookSex = ind;

                }
            },
            bookFontCup(str){//字数筛查
                if(str == 'all'){
                    this.typeArr.font = [];

                } else if(str == 300){
                    this.typeArr.font = [];
                    this.booksCopy.forEach((item) => {
                        if (parseFloat(item.showTotalCount) < str){
                            this.typeArr.font.push(item.id)
                        }
                    })
                    
                } else if(str == 500){
                    this.typeArr.font = [];
                    this.booksCopy.forEach((item) => {
                        if (parseFloat(item.showTotalCount) < str && parseFloat(item.showTotalCount) > 300){
                            this.typeArr.font.push(item.id)
                        }
                    })
                    
                } else if(str == 1000){
                    this.typeArr.font = [];
                    this.booksCopy.forEach((item) => {
                        if (parseFloat(item.showTotalCount) < str && parseFloat(item.showTotalCount) > 500){
                            this.typeArr.font.push(item.id)
                        }
                    })
                    
                } else {
                    this.typeArr.font = [];
                    this.booksCopy.forEach((item) => {
                        if (parseFloat(item.showTotalCount) > 1000){
                            this.typeArr.font.push(item.id)
                        }
                    })

                }
                console.log(this.typeArr.font);
                this.bookSift()

            },
            bookPayMoney(str){//收费筛查
                if (str == 'all') {
                    this.typeArr.charge = [];

                } else if (str == 'free') {
                    this.typeArr.charge = [];
                    this.booksCopy.forEach((item) => {
                        if (item.free4LimitedTime == true) {
                            this.typeArr.charge.push(item.id)
                        }
                    })

                } else {
                    this.typeArr.charge = [];
                    this.booksCopy.forEach((item) => {
                        if (item.free4LimitedTime == false) {
                            this.typeArr.charge.push(item.id)
                        }
                    })

                }
                this.bookSift()
            },
            bookPayType(str){//状态筛查
                if (str == 'all') {
                    this.typeArr.type = [];

                } else if (str == 'open') {
                    this.typeArr.type = [];
                    this.booksCopy.forEach((item) => {
                        if (item.bookStatus == 0) {
                            console.log(item.bookStatus);
                            this.typeArr.type.push(item.id)
                        }
                    })

                } else {
                    this.typeArr.type = [];
                    this.booksCopy.forEach((item) => {
                        if (item.bookStatus == 1) {
                            this.typeArr.type.push(item.id)
                        }
                    })

                }
                this.bookSift()
            },
            bookSift(){//完成筛查并返回数组[多项数据合并筛查]
                let n = 0;//判断用户选择的几项条件筛查
                let objArr = [];
                for (const key in this.typeArr) {
                    if(this.typeArr[key].length != 0){
                        n++
                    }
                    objArr.push(this.typeArr[key])
                }
                console.log(objArr,'objArr');
                if(n == 1){
                    // 只有一项条件筛查则不执行
                    this.bookScreen()
                    return 
                } else if(n == 0){
                    this.booksData = this.booksCopy;
                    //用户放开条件，全部选择不限
                    return
                }
                //根据用户的条件筛选数据
                let array = [];//存储满足用户的条件的书本下标
                for (let i = 0; i < objArr.length; i ++){
                     
                    var arrMerge = this.fun(array, objArr[i]);
                    if (arrMerge.length == 0){
                        continue
                    } else {
                        array = this.fun(array, objArr[i]);//对数组的去重
                    }
                }

                this.booksData = [];
                this.booksCopy.forEach((item,index) => {
                    if (array.includes(index) == true){
                        this.booksData.push(item);
                    }
                })


            },
            bookScreen(){//单项条件时执行
                let arr = []
                let isArr = arr.concat(this.typeArr.font, this.typeArr.charge, this.typeArr.type);
                this.booksData = [];
                this.booksCopy.forEach((item, index) => {
                    if (isArr.includes(index) == true) {
                        this.booksData.push(item);
                    }
                })
            },
            fun(oldarr,nextarr){//完成数组去重
                let arr = [];
                let array = [];
                if(nextarr.length == 0 || oldarr.length == 0){//新旧数组不重复
                    let isArr = arr.concat(oldarr, nextarr);
                    return isArr

                } else {
                    let isArr = arr.concat(oldarr,nextarr);//新旧数组合并
                    for(let i = 0; i < isArr.length; i++){
                        if(arr.includes(isArr[i]) == false){
                            arr.push(isArr[i]);//去重
                            
                        }else {
                            array.push(isArr[i])//取重
                            
                        }
                    }

                    return array
                }
            },
            topShow() {
                this.isTop = !this.isTop
            }
        }
    }
</script>

<style lang="scss" scoped>
.nav {
    background-color: #fff;

    .nav_wrap {
        padding: 0 14px;
        border-bottom: 1px solid #e0e0e0;
        display: flex;

        .quit {
            width: 10px;
            height: 45px;
            background-image: url('../assets/images/quit.png');
            background-repeat: no-repeat;
            background-size: 100%;
            background-position: center;
        }

        span {
            color: #333;
            font-size: 16px;
            display: block;
            text-align: center;
            line-height: 45px;
            width: 54%;
            padding-left: 70px;
        }

        .nav_icon {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 80px;

            a {
                display: block;
                width: 25px;
                height: 22px;
                background-image: url('../assets/images/book_ico1.png');
                background-repeat: no-repeat;
                background-size: 100%;
                background-position: center;
            }

            i {
                display: block;
                width: 20px;
                height: 20px;
                background-image: url('../assets/images/bookMore_ico1.png');
                background-size: 100%;
                background-position: center;

            }

            .topnav {
                background-image: url('../assets/images/tp_close.png');
            }
        }
    }
}

.wrap {
    position: fixed;
    top: 45px;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, .8);

    .subnav {
        width: 100%;
        background-color: #fff;
        padding-bottom: 18px;

        ul {
            display: flex;
            flex-wrap: wrap;

            li {
                width: 25%;
                text-align: center;
                padding-top: 20px;

                img {
                    width: 40px;
                    height: 40px;
                    margin: 0 auto 8px auto;
                }

                span {
                    font-size: 13px;
                    color: #666;
                }
            }
        }
    }

}
.bookType{
    padding: 0 14px;
    &>ul{
        padding: 20px 0 9px 0;
        li{
            display: flex;
            font-size: 14.5px;
            padding-bottom: 2px;
            .tit{
                color: #333;
                text-align: center;
                width: 44px;
                flex: 0 0 44px;
            }
            ul{
                display: flex;
                flex-wrap: wrap;
                li{
                    padding-right: 12px;
                    padding-bottom: 12px;
                    span{
                        border-radius: 4px;
                        display: block;
                        color: #999;
                        padding: 0 3.5px;
                    }
                    
                }
                &>.con{
                    span{
                        color: #fff;
                        background-color: #ea4136;
                    }
                }
            }
        }
    }
}
.bor_bot {
    border-bottom: 10px solid #eaeaea;
}
.list_books {
    padding: 0 14px;

    .books_cont {
        .sort_uls {
            padding-bottom: 0px;
        }

        ul {
            padding-bottom: 28px;

            li {
                display: flex;
                padding-top: 24px;

                .img {
                    flex: 1;

                    img {
                        width: 100px;
                        height: 140px;
                    }
                }

                .intro {
                    flex: 2;
                    padding-left: 5px;

                    .in_tit {
                        height: 47px;
                        color: #666;
                        font-size: 18px;
                    }

                    &>p {
                        height: 47px;
                        line-height: 22px;
                        font-size: 13px;
                        color: #999;
                        text-indent: 26px;
                        overflow: hidden;
                    }
                }

                .in_foot {
                    display: flex;
                    justify-content: space-between;
                    padding-top: 29px;

                    span {
                        border: 1px solid #fff;
                        display: block;
                        font-size: 12px;
                        height: 10px;
                        line-height: 10px;
                        padding: 2px 3.5px;
                    }

                    .state {
                        span {
                            border-color: #ea4136;
                            color: #ea4136;
                        }
                    }

                    .book_type {
                        display: flex;

                        .book_num {
                            border-color: #f39800;
                            color: #f39800;
                            margin-right: 6px;
                        }

                        span {
                            border-color: #706262;
                            color: #706262;
                        }
                    }
                }
            }
        }
    }

}
.search {
    padding: 10px 14px;

    .search_wrap {
        display: flex;
        padding: 4px 0 4px 17px;
        border: 1px solid #dbdbdb;
        height: 32px;
        border-radius: 20px;
        overflow: hidden;

        &>input {
            border: 0px;
            outline: none;
            flex: 6;
            color: #999;
        }

        &>button {
            flex: 1;
            background-image: url('../assets/images/searchBar.png');
            background-repeat: no-repeat;
            border: 0px;
            background-color: #fff;
            position: relative;
            border-left: 1px solid #dbdbdb;
            background-size: 18px;
            background-position: center;
        }
    }
}
.footer {
    padding: 16px 0px 32px 0px;
    background-color: #1c1a1a;

    .foot_tit {
        padding-bottom: 15px;

        ul {
            display: flex;

            li {
                flex: 1;

                a {
                    display: block;
                    font-size: 16px;
                    color: #b6b6b6;
                    text-align: center;
                    border-right: 1px solid #999999;

                }

                &:last-of-type {
                    a {
                        border: 0px;
                    }
                }
            }
        }
    }

    .foot_cont {
        display: flex;
        justify-content: space-between;
        padding: 0 16px;

        .foot_info {
            p {
                color: #b6b6b6;
                font-size: 12px;
                line-height: 28px;
            }
        }

        .info_img {
            img {
                width: 98px;
                height: 98px;
            }

            span {
                font-size: 12px;
                display: block;
                text-align: center;
                color: #b6b6b6;
                padding-top: 3px;
            }
        }
    }
}
</style>